/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbAreaTool.cpp		*/
/********************************************************/

#include <getopt.h>

#include "GlbRtbArea.h"

int main(int argc, char* argv[])
{
 	int iIndex = 0;
	int iReturn = 0;
	CGlbRtbArea CArea;
	char szArea[GLB_BYTE16] = {0};
	struct option stOpt[] = {
		{"reset", 0, 0, 'r'},
		{"delete", 0, 0, 'd'},
		{"create", 1, 0, 'c'},
		{"select", 1, 0, 's'},
		{"version", 0, 0, 'v'},
		{"help", 0, 0, 'h'},
		{0, 0, 0, 0},
	};

	if (GLB_SUCCESS == CArea.GlbRtbBaseCreate(ftok(GLB_RR_PATH, GLB_RR_KEY), GLB_RR_SUM, GLB_RR_SIZE, NULL)) {
		while (GLB_FAILURE != (iReturn = getopt_long(argc, argv, "rdc:s:vh", stOpt, &iIndex))) {
			switch (iReturn) {
				case 'r':
					CArea.GlbRtbBaseReset(GLB_RR_SUM);
					break;
				case 'd':
					CArea.GlbRtbBaseDelete();
					break;
				case 'c':
					CArea.GlbRtbBaseReset(GLB_RR_SUM);
					CArea.GlbRtbBaseInput((void*)optarg);
					break;
				case 's':
					CArea.GlbRtbAreaGet(optarg, szArea);
					if (strlen(szArea) > 0) {
						GLB_PRINT("Addr: %s -> Area: %s\n", optarg, szArea);
					}
					else {
						GLB_PRINT("Addr: %s -> Area: NULL\n", optarg);
					}
					break;
				case 'v':
					GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
					GLB_PRINT("GlbRtbAreaTool Build on %s %s.\n", __DATE__, __TIME__);
					return GLB_SUCCESS;
				case 'h':
					GLB_PRINT("Usage: %s [options]\n", argv[0]);
					GLB_PRINT("Valid options are:\n");
					GLB_PRINT("[0] -r or --reset:      reset memory\n");
					GLB_PRINT("[0] -c or --create:     create memory from advs\n");
					GLB_PRINT("[0] -s or --select:     select advs from memory\n");
					GLB_PRINT("[0] -d or --delete:     delete advs from memory\n");
					GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
					GLB_PRINT("GlbRtbAreaTool Build on %s %s.\n", __DATE__, __TIME__);
					return GLB_SUCCESS;
				default:
					return GLB_SUCCESS;
			}
		}
	}
	else {
		GLB_ERROR("Failed to GlbRtbBaseCreate\n");
	}

	return GLB_SUCCESS;
}
